<?php
namespace app\admin\controller;

use app\admin\model\Order as OrderModel;

class Index extends Base
{
    public function index()
    {
        $user_count = \app\admin\model\User::where('status', '=', 1)->count();
        $this->assign('user_count', $user_count);
        $order_count = OrderModel::count();
        $this->assign('order_count', $order_count);

        $amount = OrderModel::where('status', '=', 2)->sum('amount');
        $fee = OrderModel::where('status', '=', 2)->sum('fee');
        $this->assign('order_money', sprintf('%.2f', $amount+$fee));

        $borrow_count = \app\admin\model\Borrow::where('status', 'neq', 2)->count();
        $this->assign('borrow_count', $borrow_count);

        $year = date('Y', time());
        $years = array();
        for ($i = $year; $i > $year-5; $i--) {
            $years[] = $i;
        }
        $this->assign('year', $years);
        $this->assign('month', date('m', time()));
        return $this->fetch();
    }

    public function data()
    {
        if ($this->request->isAjax()) {
            $year = $this->request->get('year', date('Y', time()));
            $month = $this->request->get('month', '');
            $month = $month < 10 ? '0'.$month : $month;
            $labels = $data = array();
            $label = $year.'年';
            if ($month) {
                $day = date('t', strtotime($year.'-'.$month));
                for ($i = 1; $i <= $day; $i++) {
                    $labels[] = $i.'日';
                    $date = $year.'-'.$month.'-'.($i < 10 ? '0'.$i : $i);
                    $data[] = $this->getOrderMoneyByDate($date);
                }
                $label .= $month.'月';
            }else{
                for ($i = 1; $i < 13; $i++) {
                    $labels[] = $i.'月';
                    $date = $year.'-'.($i < 10 ? '0'.$i : $i);
                    $data[] = $this->getOrderMoneyByDate($date, 1);
                }
            }
            $label .= '交易报表';
            $this->success('获取成功', '', ['labels' => $labels, 'datasets' => [['label' => $label, 'data' => $data, 'borderWidth' => 2, 'borderColor' => 'rgb(255, 99, 132)', 'fill' => false,'tension' => 0]]]);
        }
    }

    /**
     * Note: 获取报表数据
     * User: Mr.Yang
     * @param $date
     * @param int $flag
     * @return string
     */
    private function getOrderMoneyByDate($date, $flag = 0)
    {
        $format = $flag == 0 ? '%Y-%m-%d' : '%Y-%m';
        $order = OrderModel::where("DATE_FORMAT(update_entered, '$format') = '$date'")->select()->toArray();
        $money = 0;
        foreach ($order as $k => $v) {
            $money += ($v['amount'] + $v['fee']);
        }
        return sprintf('%.2f', $money);
    }

    /**
     * 登录
     * @return mixed
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');

            $data = [
                'username' => $username,
                'password' => $password
            ];
            $result = $this->validate($data, 'app\admin\validate\Member.login');

            if ($result !== true) {
                $this->error($result);
            }

            $member = (new \app\admin\model\Member)->login($data);

            if ($member === false) {
                $this->error('账号或密码不正确');
            }
            session('uid', $member['id']);
            session('userinfo', $member);
            $this->redirect(url('index'));
        }
        return $this->fetch();
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        session('uid', 0);
        //Cache::store('redis')->clear();
        $this->redirect('login');
    }
}
